// pages/zhijiao/zhijiao.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: '',
    ap: '',
    list: [],
    hiddenmodalput: true,
    max: 120,
    isScroll: true,
    currentWordNumber:0,
    focus: false
  },
  onLoad:function() {
    var that = this
    // 一些不要渲染的全局变量
    that.touch = {}
    that.friend_id = 0
    this.nickname = wx.getStorageSync('userInfo').wxInfo.nickName
    // that._getFriendsList().then((res) => {
    //   that.setData({list: that._normalizeList(res.data.data)})
    // })

    try {
      var res = wx.getSystemInfoSync()
      this.pixelRatio = res.pixelRatio;
      // this.apHeight = 32 / this.pixelRatio;
      // this.offsetTop = 160 / this.pixelRatio;
      this.apHeight = 16;
      this.offsetTop = 80;
      this.setData({windowHeight: res.windowHeight + 'px'})
    } catch (e) {

    }
  },
  onShow: function () {
    let that = this
    that._getFriendsList().then((res) => {
      that.setData({list: that._normalizeList(res.data.data)})
    })
  },
  onShareAppMessage: function () {
    return {
      title: `${this.nickname}邀你吐槽他`,
      path: `booleans_zhijiao/index/index?id=${app.globalData.i_id}`,
      imageUrl: "../../images/sample.jpg"
    }
  },
  touchstart(e) {
    let anchorIndex = e.target.dataset.index
    this.setData({
      ap: this.data.list[anchorIndex].token
    })
    let firstTouch = e.touches[0]
    this.touch.anchorIndex = anchorIndex
    this.touch.y1 = firstTouch.pageY

  },
  touchmove(e) {
    let firstTouch = e.touches[0]
    this.touch.y2 = firstTouch.pageY
    let delta = (this.touch.y2 - this.touch.y1) / 14 | 0
    if (this.touch.delta !== delta) {
      //振动
      wx.vibrateShort()
    }
    this.touch.delta = delta
    let anchorIndex = this.touch.anchorIndex + delta
    if (anchorIndex < 0) anchorIndex = 0
    if (anchorIndex > this.data.list.length - 1) anchorIndex = this.data.list.length - 1
    this.setData({
      ap: this.data.list[anchorIndex].token
    })


  },
  touchend(e) {

    // this.setData({
    //   canScroll: true
    // })
    // console.log(e)
  },
  messageInput(e) {
    // console.log(e.currentTarget.dataset.id)
    let id = e.currentTarget.dataset.id
    if (id !== this.friend_id) {
      // console.log('buyiyang ')
      this.setData({
        commentInputs: ''
      })
    }
    this.friend_id = id
    this.setData({
      hiddenmodalput: false,
      isScroll: false,
      focus: true
    });
    // if (this.openParentIndex !== undefined && this.openIndex !== undefined) {
    //   this.clearOPen()
    // }
  },
  cancel: function(){
    this.setData({
      hiddenmodalput: true,
      isScroll: true,
      focus: false
    });
  },
  // 提交
  confirm: function(){
    let that = this
    if (that.data.commentInputs === '') {
      wx.showToast({
        title: '请输入吐槽内容',
        icon: 'none'
      })
      return
    }
    app.util.request({
      url: 'entry/wxapp/givecomment',
      method: 'POST',
      data: {
        friend_id: that.friend_id,
        content: that.data.commentInputs
      },
      success: (res) => {
        console.log(res)
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true,
          success: () => {
            that._getFriendsList().then((res) => {
              that.setData({
                list: that._normalizeList(res.data.data),
                commentInputs: '',
                currentWordNumber: 0
              })
            })
          }
        })
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true,
          success: () => {
            that._getFriendsList().then((res) => {
              that.setData({
                list: that._normalizeList(res.data.data),
                commentInputs: '',
                currentWordNumber: 0
              })
            })
          }
        })
      }
    })

    this.setData({
      hiddenmodalput: true,
      isScroll: true,
      focus: false
    })
  },
  commentInputs: function (e) {
    // 获取输入框的内容
    console.log(e)
    var value = e.detail.value
    this.setData({
      commentInputs: e.detail.value
    })
    // 获取输入框内容的长度
    var len = parseInt(value.length)
    this.setData({
      currentWordNumber: len //当前字数
    });
  },
  handleSearch () {
    wx.navigateTo({
      url: '../search/search'
    })
  },
  scrollTo(id) {
    this.setData({
      ap: id
    })
  },
  showModal(msg) {
    wx.showModal({
      title: msg,
      showCancel: false
    })
  },
  touchS(e) {
    if (e.touches.length == 1) {
      // 设置活动起始点
      this.tmpStartx = e.touches[0].clientX;
    }
  },
  // touchM(e) {
  //   // console.log(e)
  //   // 如果有已经打开的举报按钮，就关掉
  //   if (this.openParentIndex !== undefined && this.openIndex !== undefined && ( this.openParentIndex != e.currentTarget.dataset.parentindex || this.openIndex != e.currentTarget.dataset.index )) {
  //     this.clearOPen()
  //   }
  //   let index = e.currentTarget.dataset.index
  //   let parentIndex = e.currentTarget.dataset.parentindex
  //   // console.log('index: ', index, ' parentIndex: ', parentIndex)
  //   let disX = parseInt(this.tmpStartx) - parseInt(e.touches[0].clientX)
  //   if (disX >= 0) {
  //     // 左滑动
  //     // 如果已经滑出
  //     if (this.data.list[parentIndex].itemList[index].isDelShow) return false;
  //     // 未滑出时，滑出距离小于删除按钮宽度时修改item的right
  //     // console.log(disX);
  //     if (disX <= this.getDelBtnWidth()) {
  //       // this.data.commentList[index].right = disX;
  //       this.setData({
  //         [`list[${parentIndex}].itemList[${index}].right`]: disX
  //       })
  //     }
  //   } else {
  //     // 右滑动
  //     // 如果已经滑出,
  //     if (this.data.list[parentIndex].itemList[index].isDelShow) {
  //       if (Math.abs(disX) <= this.getDelBtnWidth()) {
  //         // this.data.commentList[index].right = this.getDelBtnWidth() + disX;
  //         this.setData({
  //           [`list[${parentIndex}].itemList[${index}].right`]: this.getDelBtnWidth() + disX
  //         })
  //       }
  //     }
  //   }
  // },
  // touchE(e) {
  //   // 如果有已经打开的举报按钮，就关掉
  //   if (this.openParentIndex !== undefined && this.openIndex !== undefined && ( this.openParentIndex != e.currentTarget.dataset.parentindex || this.openIndex != e.currentTarget.dataset.index )) {
  //     this.clearOPen()
  //   }
  //   let index = e.currentTarget.dataset.index
  //   let parentIndex = e.currentTarget.dataset.parentindex
  //   let disX = parseInt(this.tmpStartx) - parseInt(e.changedTouches[0].clientX)
  //   if (disX >= 0) {
  //     // 左滑动
  //     // 如果未滑出
  //     if (!this.data.list[parentIndex].itemList[index].isDelShow) {
  //       if (disX >= (this.getDelBtnWidth() / 2)) {
  //         // 滑动距离大于按钮一半，按钮完全显示
  //         // this.listData[index].right = this.getDelBtnWidth();
  //         // this.listData[index].isDelShow = true;
  //         this.setData({
  //           [`list[${parentIndex}].itemList[${index}].right`]: this.getDelBtnWidth(),
  //           [`list[${parentIndex}].itemList[${index}].isDelShow`]: true
  //         })
  //         this.openParentIndex = parentIndex
  //         this.openIndex = index
  //       } else {
  //         // 滑动距离小于按钮一半，按钮隐藏
  //         // this.listData[index].right = 0;
  //         // this.listData[index].isDelShow = false;
  //         this.setData({
  //           [`list[${parentIndex}].itemList[${index}].right`]: 0,
  //           [`list[${parentIndex}].itemList[${index}].isDelShow`]: false
  //         })
  //         this.openIndex = undefined
  //         this.openParentIndex = undefined
  //       }
  //     }
  //   } else {
  //     // 右滑动
  //     // 如果已经滑出
  //     if (this.data.list[parentIndex].itemList[index].isDelShow) {
  //       if (Math.abs(disX) >= (this.getDelBtnWidth() / 2)) {
  //         // this.listData[index].right = 0;
  //         // this.listData[index].isDelShow = false;
  //         this.setData({
  //           [`list[${parentIndex}].itemList[${index}].right`]: 0,
  //           [`list[${parentIndex}].itemList[${index}].isDelShow`]: false
  //         })
  //         this.openIndex = undefined
  //         this.openParentIndex = undefined
  //       } else {
  //         // this.listData[index].right = this.getDelBtnWidth();
  //         // this.listData[index].isDelShow = true;
  //         this.setData({
  //           [`list[${parentIndex}].itemList[${index}].right`]: this.getDelBtnWidth(),
  //           [`list[${parentIndex}].itemList[${index}].isDelShow`]: true
  //         })
  //         this.openParentIndex = parentIndex
  //         this.openIndex = index
  //       }
  //     }
  //   }
  // },
  // getDelBtnWidth() {
  //   let btnWidth_rpx = 200; // 按钮的rpx宽度
  //   let res = wx.getSystemInfoSync();
  //   return (res.windowWidth / 750) * btnWidth_rpx;
  // },
  // clearOPen() {
  //   this.setData({
  //     [`list[${this.openParentIndex}].itemList[${this.openIndex}].right`]: 0,
  //     [`list[${this.openParentIndex}].itemList[${this.openIndex}].isDelShow`]: false
  //   })
  //   this.openIndex = undefined
  //   this.openParentIndex = undefined
  // },
  // del (e) {
  //   let that = this
  //   wx.showModal({
  //     title: '删除好友',
  //     content: '确定删除好友',
  //     success: function(res) {
  //       if (res.confirm) {
  //         console.log('用户点击确定')
  //         that._del(e.currentTarget.dataset.id).then((res) => {
  //           wx.showToast({
  //             title: res.data.message,
  //             icon: "none"
  //           })
  //           that.clearOPen()
  //           that._getFriendsList().then((res) => {
  //             that.setData({list: that._normalizeList(res.data.data)})
  //           })
  //         }).catch((res) => {
  //           wx.showToast({
  //             title: res.data.message,
  //             icon: "none"
  //           })
  //           that.clearOPen()
  //           that._getFriendsList().then((res) => {
  //             that.setData({list: that._normalizeList(res.data.data)})
  //           })
  //         })
  //       } else if (res.cancel) {
  //         console.log('用户点击取消')
  //         that.clearOPen()
  //       }
  //     }
  //   })
  // },
  _normalizeList(obj) {
    var ret = []
    // console.log(obj.user)
    for (var key in obj) {
      // console.log(key)
      var item = {}
      item.token = key
      item.itemList = obj[key]
    //  console.log(item)
      ret.push(item)
    }
    // console.log(ret)
    return ret
  },
  _getFriendsList () {
    console.log('--------获取好友列表--------')
    let that = this
    return new Promise((resolve, reject) => {
      app.util.request({
        url: 'entry/wxapp/friends',
        success: function(res) {
          // console.log(res.data.data)
          // that.setData({list: that._normalizeList(res.data.data)})
          console.log('--------获取好友列表成功--------')
          console.log('res: ', res)
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },
  _del (id) {
    return new Promise((resolve, reject) => {
      app.util.request({
        url: 'entry/wxapp/deletefriend',
        data: {
          friend_id: id
        },
        success: function(res) {
          // console.log(res.data.data)
          // that.setData({list: that._normalizeList(res.data.data)})
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  }
})
